home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / quicktime / importers and exporters / carbon qt graphic import / source / mycarbonprinting.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  18.9 KB  |  609 lines

  1. /*
  2.     File:        MyCarbonPrinting.c
  3.     
  4.     Description:Code that prints pictures via the new Carbon Print Manager.
  5.  
  6.     Author:        MC
  7.  
  8.     Copyright:     © Copyright 1999-2000 Apple Computer, Inc. All rights reserved.
  9.     
  10.     Disclaimer:    IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc.
  11.                 ("Apple") in consideration of your agreement to the following terms, and your
  12.                 use, installation, modification or redistribution of this Apple software
  13.                 constitutes acceptance of these terms.  If you do not agree with these terms,
  14.                 please do not use, install, modify or redistribute this Apple software.
  15.  
  16.                 In consideration of your agreement to abide by the following terms, and subject
  17.                 to these terms, Apple grants you a personal, non-exclusive license, under Apple’s
  18.                 copyrights in this original Apple software (the "Apple Software"), to use,
  19.                 reproduce, modify and redistribute the Apple Software, with or without
  20.                 modifications, in source and/or binary forms; provided that if you redistribute
  21.                 the Apple Software in its entirety and without modifications, you must retain
  22.                 this notice and the following text and disclaimers in all such redistributions of
  23.                 the Apple Software.  Neither the name, trademarks, service marks or logos of
  24.                 Apple Computer, Inc. may be used to endorse or promote products derived from the
  25.                 Apple Software without specific prior written permission from Apple.  Except as
  26.                 expressly stated in this notice, no other rights or licenses, express or implied,
  27.                 are granted by Apple herein, including but not limited to any patent rights that
  28.                 may be infringed by your derivative works or by other works in which the Apple
  29.                 Software may be incorporated.
  30.  
  31.                 The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
  32.                 WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
  33.                 WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  34.                 PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
  35.                 COMBINATION WITH YOUR PRODUCTS.
  36.  
  37.                 IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
  38.                 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  39.                 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  40.                 ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
  41.                 OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
  42.                 (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
  43.                 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44.                 
  45.     Change History (most recent first):
  46.  
  47. */
  48.  
  49. #define TARGET_API_MAC_CARBON 1
  50.  
  51. #include <QuickTimeComponents.h>
  52. #include <Windows.h>
  53. #include "MyCarbonPrinting.h"
  54. #include "Defines.h"
  55. #include "Structs.h"
  56. #include "GraphicImportDrawCode.h"
  57. #include "LinkedList.h"
  58. #include "DisplayErrorAlert.h"
  59.  
  60. // Globals
  61. extern PreferencesHandle            gPreferences;
  62.  
  63. static void PMRectToRect (PMRect * pRect, Rect * rRect) {
  64.     rRect->top = (short)pRect->top;
  65.     rRect->bottom = (short)pRect->bottom;
  66.     rRect->left = (short)pRect->left;
  67.     rRect->right = (short)pRect->right;
  68. }
  69.  
  70. static UInt32 CountRectsInRect (Rect * pageRect, Rect * imageRect) {
  71.     UInt32                        count;
  72.     Rect                        tempPageRect,
  73.                                 tempImageRect;
  74.     UInt32                        pagesAcross,
  75.                                 pagesDown;
  76.     tempPageRect = *pageRect;
  77.     tempImageRect = *imageRect;
  78.  
  79.     OffsetRect (&tempPageRect, (short)-tempPageRect.left, (short)-tempPageRect.top);
  80.     OffsetRect (&tempImageRect, (short)-tempImageRect.left, (short)-tempImageRect.top);
  81.  
  82.     pagesAcross = (UInt32)((tempImageRect.right + (tempPageRect.right - 1)) / tempPageRect.right);
  83.     pagesDown = (UInt32)((tempImageRect.bottom + (tempPageRect.bottom - 1)) / tempPageRect.bottom);
  84.     count = pagesAcross * pagesDown;
  85.  
  86.     return count;
  87. }
  88.  
  89. static OSStatus MakeDefaultPageFormatObject (PMPageFormat * pageFormat) {
  90.     OSStatus                err;
  91.  
  92.     //    Set up a valid PageFormat object.
  93.     if (*pageFormat == kPMNoPageFormat) {
  94.         err = PMNewPageFormat (pageFormat);
  95.  
  96.         if ((err == noErr) && (*pageFormat != kPMNoPageFormat))
  97.             err = PMDefaultPageFormat (*pageFormat);
  98.     } else {
  99.         err = PMValidatePageFormat (*pageFormat, kPMDontWantBoolean);
  100.     }
  101.  
  102.     return err;
  103. }
  104.  
  105. static OSErr TileRectsInRect (Rect * pageRect, Rect * imageRect, myListPtr * overlapList) {
  106.     Rect                        tempImageRect,
  107.                                 tempPageRect,
  108.                                 overlappingRect;
  109.     Rect *                        rectPtr;
  110.     Boolean                        overlap;
  111.     OSErr                        err;
  112.  
  113.     tempImageRect = *imageRect;
  114.     tempPageRect = *pageRect;
  115.  
  116.     do {
  117.         do {
  118.             overlap = SectRect (&tempImageRect, &tempPageRect, &overlappingRect);
  119.  
  120.             if (overlap) {
  121.                 rectPtr = (Rect *)NewPtr (sizeof(Rect));
  122.                 err = MemError ();
  123.  
  124.                 if (err == noErr) {
  125.                     *rectPtr = overlappingRect;
  126.                     err = AppendToList (rectPtr, overlapList);
  127.                     OffsetRect (&tempPageRect, tempPageRect.right, 0);
  128.                 }
  129.             }
  130.         } while (overlap == true);
  131.  
  132.         tempPageRect.right = pageRect->right;
  133.         tempPageRect.left = pageRect->left;
  134.         OffsetRect (&tempPageRect, 0, tempPageRect.bottom);
  135.         overlap = SectRect (&tempImageRect, &tempPageRect, &overlappingRect);
  136.     } while (overlap == true && err == noErr);
  137.  
  138.     return err;
  139. }
  140.  
  141. /*------------------------------------------------------------------------------
  142.     Function:    DetermineNumberOfPagesInDoc
  143.     Parameters:
  144.         pageRect    - size of the document, from PMGetAdjustedPageRect
  145.     Description:
  146.         Compares the size of the document with the number of pages in the
  147.         PageFormat object to calculate the number of pages required to print
  148.         the document.
  149. ------------------------------------------------------------------------------*/
  150. static UInt32 DetermineNumberOfPagesInDoc (PMRect pageRect, WindowPtr theWindow, myListPtr * rectsList) {
  151. #pragma unused (pageRect)
  152.     WindowInfoHandle            winInfo;
  153.     SInt8                        hState;
  154.     UInt32                        pageCount;
  155.     Rect                        pRect;
  156.     OSErr                        err;
  157.  
  158.     pageCount = 0;
  159.  
  160.     if (theWindow != nil) {
  161.         winInfo = (WindowInfoHandle)GetWRefCon (theWindow);
  162.     }
  163.  
  164.     if (winInfo != nil && (**winInfo).sig == kMySig) {
  165.         hState = HGetState ((Handle)winInfo);
  166.         HLock ((Handle)winInfo);
  167.         PMRectToRect (&pageRect, &pRect);
  168.         pageCount = CountRectsInRect (&pRect, &(**winInfo).winNatGraphicBoundsRect);
  169.  
  170.         if (pageCount >= 1) {
  171.             err = TileRectsInRect (&pRect, &(**winInfo).winNatGraphicBoundsRect, rectsList);
  172.         }
  173.  
  174.         HSetState ((Handle)winInfo, hState);
  175.     }
  176.  
  177.     return pageCount;
  178. }
  179.  
  180. static OSStatus DrawIntoPrintPort (WindowInfoHandle winInfo, Rect * sourceRect) {
  181.     OSStatus                    err;
  182.     GraphicsImportComponent        graphicImporter;
  183.     Rect                        tempRect;
  184.  
  185.     if ((**winInfo).winDataHandle != nil) {
  186.         graphicImporter = OpenDefaultComponent (GraphicsImporterComponentType, (**winInfo).winGraphicType);
  187.  
  188.         if (graphicImporter != nil) {
  189.             err = GraphicsImportSetDataHandle (graphicImporter, (**winInfo).winDataHandle);
  190.         }
  191.     } else {
  192.         err = GetGraphicsImporterForFile (&(**winInfo).winSpec, &graphicImporter);
  193.     }
  194.  
  195.     if (err == noErr) {
  196.         err = GraphicsImportGetSourceRect (graphicImporter, &tempRect);
  197.     }
  198.  
  199.     if (err == noErr) {
  200.         err = GraphicsImportSetSourceRect (graphicImporter, sourceRect);
  201.     }
  202.  
  203.     if (err == noErr) {
  204.         OffsetRect (&tempRect, (short)-sourceRect->left, (short)-sourceRect->top);
  205.         err = GraphicsImportSetBoundsRect (graphicImporter, &tempRect);
  206.     }
  207.  
  208.     if (err == noErr) {
  209.         (void)GraphicsImportSetQuality ((**winInfo).winGraphicImporter, (**gPreferences).quality);
  210.     }
  211.  
  212.     if (err == noErr) {
  213.         err = GraphicsImportDraw (graphicImporter);
  214.     }
  215.  
  216.     if (graphicImporter != nil) {
  217.         (void)CloseComponent (graphicImporter);
  218.     }
  219.  
  220.     return err;
  221. }
  222.  
  223. #define PREVIEWPRINTING 0
  224.  
  225. static OSErr PrintPage (WindowPtr theWindow, PMRect pageRect, PMPrintContext printingPort, UInt32 pageNumber, Rect * printRect) {
  226. #pragma unused (pageNumber)
  227.     OSStatus                    err;
  228.     WindowInfoHandle            winInfo;
  229.     SInt8                        hState;
  230.     Rect                        pRect;
  231.     PixMapHandle                pixMap;
  232. #if PREVIEWPRINTING
  233.     WindowPtr                    testWindow;
  234.     GrafPtr                        oldPort;
  235.     Rect                        winBounds;
  236. #pragma unused (printingPort)
  237. #else
  238.     GrafPtr                        pmPort;
  239. #endif
  240.  
  241.     if (theWindow != nil) {
  242.         winInfo = (WindowInfoHandle)GetWRefCon (theWindow);
  243.     }
  244.  
  245.     if (winInfo != nil && (**winInfo).sig == kMySig) {
  246.         hState = HGetState ((Handle)winInfo);
  247.         HLock ((Handle)winInfo);
  248.  
  249.         PMRectToRect (&pageRect, &pRect);
  250.         pRect.right = (short)(printRect->right - printRect->left);
  251.         pRect.bottom = (short)(printRect->bottom - printRect->top);
  252.  
  253.         if ((**winInfo).winGWorld != nil) {
  254.             pixMap = GetGWorldPixMap ((**winInfo).winGWorld);
  255.             if (LockPixels (pixMap)) {
  256. #if PREVIEWPRINTING
  257.                 GetPort (&oldPort);
  258.                 testWindow = NewWindow(nil, &pRect, "\ptest", true, 0, (WindowPtr)-1L, true, 0);
  259.                 SetPort (GetWindowPort(testWindow));
  260.                 GetWindowPortBounds (testWindow, &winBounds);
  261.                 CopyBits (GetPortBitMapForCopyBits ((**winInfo).winGWorld),
  262.                             GetPortBitMapForCopyBits (GetWindowPort(testWindow)),
  263.                             printRect, &pRect, srcCopy, nil);
  264.                 while (!Button ()) {}
  265.  
  266.                 DisposeWindow (testWindow);
  267.                 SetPort (oldPort);
  268. #else
  269.                 PMGetGrafPtr (printingPort, &pmPort);
  270.                 CopyBits ((BitMap*)*GetPortPixMap ((**winInfo).winGWorld),
  271.                             (BitMap*)*GetPortPixMap (pmPort),
  272.                             printRect, &pRect, srcCopy, nil);
  273. #endif
  274.                 UnlockPixels (pixMap);
  275.             } else {
  276.                 DisposeGWorld ((**winInfo).winGWorld);
  277.                 (**winInfo).winGWorld = nil;
  278.                 err = DrawIntoPrintPort (winInfo, printRect);
  279.             }
  280.         } else {
  281. #if PREVIEWPRINTING
  282.             GetPort (&oldPort);
  283.             testWindow = NewWindow(nil, &pRect, "\ptest", true, 0, (WindowPtr)-1L, true, 0);
  284.             SetPort (GetWindowPort(testWindow));
  285.             GetWindowPortBounds (testWindow, &winBounds);
  286.             err = DrawIntoPrintPort (winInfo, printRect);
  287.             while (!Button ()) {}
  288.             DisposeWindow (testWindow);
  289.             SetPort (oldPort);
  290. #else
  291.             err = DrawIntoPrintPort (winInfo, printRect );
  292. #endif
  293.         }
  294.  
  295.         HSetState ((Handle)winInfo, hState);
  296.     }
  297.  
  298.     return (OSErr)err;
  299. }
  300.  
  301. static OSErr PrintWindow (WindowPtr theWindow, PMPageFormat * pageFormat, PMPrintSettings * printSettings) {
  302.     OSStatus                    err,
  303.                                 printError;
  304.     PMRect                        pageRect;
  305.     PMPrintContext                thePrintingPort;
  306.     UInt32                        realNumberOfPagesinDoc,
  307.                                 pageNumber,
  308.                                 firstPage,
  309.                                 lastPage;
  310.     myListPtr                    rectsList;
  311.     Rect *                        printRect;
  312.  
  313.     thePrintingPort = kPMNoReference;
  314.  
  315.     //    PMGetAdjustedPageRect returns the page size taking into account rotation,
  316.     //    resolution and scaling settings.  DetermineNumberOfPagesInDoc returns the
  317.     //    number of pages required to print the document.
  318.     err = PMGetAdjustedPageRect (*pageFormat, &pageRect);
  319.  
  320.     if (err == noErr) {
  321.         rectsList = nil;
  322.         realNumberOfPagesinDoc = DetermineNumberOfPagesInDoc (pageRect, theWindow, &rectsList);
  323.     }
  324.  
  325.     //    Get the user's selection for first and last pages
  326.     if (err == noErr) {
  327.         err = PMGetFirstPage (*printSettings, &firstPage);
  328.         if (err == noErr) {
  329.             err = PMGetLastPage (*printSettings, &lastPage);
  330.         }
  331.     }
  332.  
  333.     //    Check that the selected page range does not go beyond the actual
  334.     //    number of pages in the document.
  335.     if (err == noErr) {
  336.         if (realNumberOfPagesinDoc < lastPage) {
  337.             lastPage = realNumberOfPagesinDoc;
  338.         }
  339.     }
  340.  
  341.     //    NOTE:  We don't have to worry about the number of copies.  The Printing
  342.     //    Manager handles this.  So we just iterate through the document from the
  343.     //    first page to be printed, to the last.
  344.     if (err == noErr) {
  345.         //    Establish a printing port for drawing.
  346.         err = PMBeginDocument (*printSettings, *pageFormat, &thePrintingPort);
  347.         if ((err == noErr) && (thePrintingPort != kPMNoReference)) {
  348.             //    Print the selected range of pages in the document.
  349.             pageNumber = firstPage;
  350.             while ((pageNumber <= lastPage) && (err == noErr) && (PMError () == noErr)) {
  351.                 //    NOTE:  We don't have to deal with the old Printing Manager's
  352.                 //    128-page boundary limit anymore.
  353.                 //    Establish a printing page.
  354.                 err = PMBeginPage (thePrintingPort, nil);
  355.  
  356.                 if (err == noErr) {
  357.                     //    Draw the page.
  358.                     printRect = (Rect *)GetItemNumFromList (pageNumber - 1, rectsList);
  359.                     err = PrintPage (theWindow, pageRect, thePrintingPort, pageNumber, printRect);
  360.                 }
  361.                 //    Close the page.  Have to call PMEndPage if we called PMBeginPage
  362.                 err = PMEndPage (thePrintingPort);
  363.  
  364.                 if (err == noErr) {
  365.                     //    And loop.
  366.                     pageNumber++;
  367.                 }
  368.             }
  369.  
  370.             // Close the printing port
  371.             (void)PMEndDocument (thePrintingPort);
  372.         }
  373.     }
  374.  
  375.     //    Only report a printing error once we have completed the print loop. This
  376.     //    ensures that every PMBeginXXX call is followed by a matching PMEndXXX
  377.     //    call, so the Printing Manager can release all temporary memory and close
  378.     //    properly.
  379.     printError = PMError ();
  380.  
  381.     if (printError != noErr) {
  382.         DisplayErrorAlert ((OSErr)printError);
  383.     }
  384.  
  385.     do {
  386.         printRect = (Rect *)GetItemNumFromList (0, rectsList);
  387.         DeleteHeadFromList (&rectsList);
  388.     } while (printRect != nil);
  389.  
  390.     return (OSErr)err;
  391. }
  392.  
  393. /*------------------------------------------------------------------------------
  394.     Function:
  395.         FlattenAndSavePageFormat
  396.     Parameters:
  397.         pageFormat    -    a PageFormat object
  398.     Description:
  399.         Flattens a PageFormat object so it can be saved with the document.
  400.         Assumes caller passes a validated PageFormat object.
  401. ------------------------------------------------------------------------------*/
  402. static OSStatus FlattenAndSavePageFormat (PMPageFormat pageFormat, Handle * flatFormatHandle) {
  403.     OSStatus                err;
  404.  
  405.     //    Flatten the PageFormat object to memory.
  406.     err = PMFlattenPageFormat (pageFormat, flatFormatHandle);
  407.  
  408.     return err;
  409. }
  410.  
  411. /*------------------------------------------------------------------------------
  412.     Function:    LoadAndUnflattenPageFormat
  413.     Parameters:
  414.         pageFormat    - PageFormat object read from document file
  415.     Description:
  416.         Gets flattened PageFormat data from the document and returns a PageFormat
  417.         object.
  418. ------------------------------------------------------------------------------*/
  419. static OSStatus LoadAndUnflattenPageFormat (PMPageFormat * pageFormat, Handle flatFormatHandle) {
  420.     OSStatus                err;
  421.  
  422.     //    Convert the PageFormat flattened data into a PageFormat object.
  423.     err = PMUnflattenPageFormat (flatFormatHandle, pageFormat);
  424.     
  425.     return err;
  426. }
  427.  
  428. static OSStatus FlattenAndSavePrintSettings (PMPrintSettings printSettings, Handle * flatFormatHandle) {
  429.     OSStatus                err;
  430.  
  431.     //    Flatten the PrintSettings object to memory.
  432.     err = PMFlattenPrintSettings (printSettings, flatFormatHandle);
  433.  
  434.     return err;
  435. }
  436.  
  437. static OSStatus LoadAndUnflattenPrintSettings (PMPrintSettings * printSettings, Handle flatFormatHandle) {
  438.     OSStatus                err;
  439.  
  440.     //    Convert the PrintSettings flattened data into a PrintSettings object.
  441.     err = PMUnflattenPrintSettings (flatFormatHandle, printSettings);
  442.     
  443.     return err;
  444. }
  445.  
  446. /*------------------------------------------------------------------------------
  447.     Function:    DoPageSetupDialog
  448.     Parameters:
  449.         pageFormat    -    a PageFormat object addr
  450.     Description:
  451.         If the caller passes in an empty PageFormat object, create a new one,
  452.         otherwise validate the one provided by the caller.
  453.         Invokes the Page Setup dialog and checks for Cancel.
  454.         Flattens the PageFormat object so it can be saved with the document.
  455.         Note that the PageFormat object is modified by this function.
  456. ------------------------------------------------------------------------------*/
  457. static OSErr DoPageSetupDialog (PMPageFormat * pageFormat, Handle * flatFormatHandle) {
  458.     OSStatus                err;
  459.     Boolean                    accepted;
  460.  
  461.     err = MakeDefaultPageFormatObject (pageFormat);
  462.  
  463.     //    Display the Page Setup dialog.
  464.     if (err == noErr) {
  465.         err = PMPageSetupDialog (*pageFormat, &accepted);
  466.         if (!accepted) {
  467.             err = kPMCancel;        // user clicked Cancel button
  468.         }
  469.     }
  470.  
  471.     //    If the user did not cancel, flatten and save the PageFormat object
  472.     //    with our document.
  473.     if (err == noErr) {
  474.         err = FlattenAndSavePageFormat (*pageFormat, flatFormatHandle);
  475.     }
  476.  
  477.     return (OSErr)err;
  478. }
  479.  
  480. /*------------------------------------------------------------------------------
  481.     Function:    DoPrintDialog
  482.     Parameters:
  483.         pageFormat        -    a PageFormat object addr
  484.         printSettings    -    a PrintSettings object addr
  485.     Description:
  486.         If the caller passes an empty PrintSettings object, create a new one,
  487.         otherwise validate the one provided by the caller.
  488.         Invokes the Print dialog and checks for Cancel.
  489.         Note that the PrintSettings object is modified by this function.
  490. ------------------------------------------------------------------------------*/
  491. static OSErr DoPrintDialog (PMPageFormat * pageFormat, PMPrintSettings * printSettings) {
  492.     OSStatus        err;
  493.     Boolean            accepted;
  494.     
  495.     //    In this sample code the caller provides a valid PageFormat reference but in
  496.     //    your application you may want to load and unflatten the PageFormat object
  497.     //    that was saved at PageSetup time.  See LoadAndUnflattenPageFormat below.
  498.     
  499.     //    Set up a valid PrintSettings object.
  500.     if (*printSettings == kPMNoPrintSettings) {
  501.         err = PMNewPrintSettings (printSettings);
  502.         
  503.         if ((err == noErr) && (*printSettings != kPMNoPrintSettings))
  504.             err = PMDefaultPrintSettings (*printSettings);
  505.     } else {
  506.         err = PMValidatePrintSettings (*printSettings, kPMDontWantBoolean);
  507.     }
  508.     
  509.     //    Display the Print dialog.
  510.     if (err == noErr) {
  511.         err = PMPrintDialog (*printSettings, *pageFormat, &accepted);
  512.         if (!accepted) {
  513.             err = kPMCancel;        // user clicked Cancel button
  514.         }
  515.     }
  516.         
  517.     return (OSErr)err;
  518. }
  519.  
  520. OSErr HandlePageSetupDialog (WindowPtr theWindow) {
  521.     OSStatus                    err;
  522.     WindowInfoHandle            winInfo;
  523.     SInt8                        hState;
  524.     PMPageFormat                pageFormat;
  525.  
  526.     err = PMBegin ();
  527.  
  528.     if (err == noErr) {
  529.         if (theWindow != nil) {
  530.             winInfo = (WindowInfoHandle)GetWRefCon (theWindow);
  531.         }
  532.  
  533.         if (winInfo != nil && (**winInfo).sig == kMySig) {
  534.             hState = HGetState ((Handle)winInfo);
  535.             HLock ((Handle)winInfo);
  536.  
  537.             if ((**winInfo).flatPageFormatHandle == nil) {
  538.                 pageFormat = kPMNoPageFormat;
  539.             } else {
  540.                 err = LoadAndUnflattenPageFormat (&pageFormat, (**winInfo).flatPageFormatHandle);
  541.             }
  542.  
  543.             err = DoPageSetupDialog (&pageFormat, &(**winInfo).flatPageFormatHandle);
  544.             HSetState ((Handle)winInfo, hState);
  545.         }
  546.     }
  547.  
  548.     (void)PMEnd ();
  549.  
  550.     return (OSErr)err;
  551. }
  552.  
  553. OSErr HandlePrint (WindowPtr theWindow) {
  554.     OSStatus                    err;
  555.     WindowInfoHandle            winInfo;
  556.     SInt8                        hState;
  557.     PMPageFormat                pageFormat;
  558.     PMPrintSettings                printSettings;
  559.  
  560.     err = (OSErr)PMBegin ();
  561.  
  562.     if (err == noErr) {
  563.         if (theWindow != nil) {
  564.             winInfo = (WindowInfoHandle)GetWRefCon (theWindow);
  565.         }
  566.  
  567.         if (winInfo != nil && (**winInfo).sig == kMySig) {
  568.             hState = HGetState ((Handle)winInfo);
  569.             HLock ((Handle)winInfo);
  570.  
  571.             if ((**winInfo).flatPageFormatHandle == nil) {
  572.                 // Just use the default Page Setup options
  573.                 pageFormat = kPMNoPageFormat;
  574.                 err = (OSErr)MakeDefaultPageFormatObject (&pageFormat);
  575.  
  576.                 if (err == noErr) {
  577.                     err = (OSErr)FlattenAndSavePageFormat (pageFormat, &(**winInfo).flatPageFormatHandle);
  578.                 }
  579.             } else {
  580.                 err = (OSErr)LoadAndUnflattenPageFormat (&pageFormat, (**winInfo).flatPageFormatHandle);
  581.             }
  582.  
  583.             if ((**winInfo).flatPrintSettingsHandle != nil) {
  584.                 err = (OSErr)LoadAndUnflattenPrintSettings (&printSettings, (**winInfo).flatPrintSettingsHandle);
  585.             } else {
  586.                 printSettings = kPMNoPrintSettings;
  587.             }
  588.  
  589.             err = DoPrintDialog (&pageFormat, &printSettings);
  590.  
  591.             //    If the user did not cancel, flatten and save the PrintSettings object
  592.             //    with our document.
  593.             if (err == noErr) {
  594.                 err = (OSErr)FlattenAndSavePrintSettings (printSettings, &(**winInfo).flatPrintSettingsHandle);
  595.             }
  596.  
  597.             if (err == noErr) {
  598.                 err = PrintWindow (theWindow, &pageFormat, &printSettings);
  599.             }
  600.  
  601.             HSetState ((Handle)winInfo, hState);
  602.         }
  603.     }
  604.  
  605.     (void)PMEnd ();
  606.  
  607.     return (OSErr)err;
  608. }
  609.